package controllers

import (
	"gitee.com/igolang/imoney/internal/common/httpResp"
	commonTools "gitee.com/igolang/imoney/internal/common/tools"
	"gitee.com/igolang/imoney/internal/common/tools/storage"
	"github.com/gin-gonic/gin"
	"net/http"
)

type cProfile struct {
}

var Profile = &cProfile{}

func (c *cProfile) Profile(ctx *gin.Context) {
	name := ctx.Param("filename")
	r := httpResp.Response()
	if name == "" {
		httpResp.ErrorResponse(httpResp.ErrCodeInvalidParams, r)
		ctx.JSON(http.StatusNotFound, r)
		return
	}

	filename, err := storage.Get(name)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		ctx.JSON(http.StatusNotFound, r)
		return
	}
	// 开始处理
	if err = commonTools.OutFile(ctx, filename); err != nil {
		httpResp.ErrorResponse(err, r)
		ctx.JSON(http.StatusNotFound, r)
		return
	}
}
